package org.tgcloud.group.core;

import io.swagger.annotations.*;
import lombok.Data;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by cloudLiu on 2015/8/3.
 */
@Data
@ApiModel("返回对象")
public class APPResultMessage<T, L> {

    public static final int WARNING = 3;

    private ResultMessage<T, L> result;

    @ApiModelProperty("消息状态")
    private int key;
    @ApiModelProperty("消息提示")
    private String message;

    public APPResultMessage()
    {

    }

    public static APPResultMessage getSuccessMessage(Object data)
    {
        APPResultMessage message = getSuccessMessage();
        ResultMessage result = new ResultMessage();
        if (data instanceof List)
        {
            result.setList((List) data);
        } else if (data instanceof Map)
        {
//            result.setMap((Map) data);
        }
        else
        {
            result.setData(data);
        }
        message.setResult(result);
        return message;
    }

    public APPResultMessage(int key, String message)
    {
        this.key = key;
        this.message = message;
    }

    public static APPResultMessage IDENTIFY_ERROR()
    {
        return new APPResultMessage(1202,"验证码错误");
    }

    public static APPResultMessage TIME_OUT()
    {
        return new APPResultMessage(1201,"验证码超时");
    }

    public static APPResultMessage FILE_TOO_LARGE()
    {
        return new APPResultMessage(1200,"上传文件过大");
    }

    public static APPResultMessage MISS_PARAMETER()
    {
        return new APPResultMessage(1005,"参数不全");
    }

    public static APPResultMessage TYPE_MISMATCH()
    {
        return new APPResultMessage(1002,"参数格式有误");
    }

    public static APPResultMessage getSuccessMessage()
    {
        return new APPResultMessage(1000,"成功");
    }

    public static APPResultMessage SYSTEM_BUSY() {
        return new APPResultMessage(3333,"系统繁忙，请稍后重试");
    }
    public static APPResultMessage COMMIT_BUSY() {
        return new APPResultMessage(4001,"已提交");
    }

    public static APPResultMessage SYSTEM_ERROR() {
        return new APPResultMessage(2000,"出现异常，及时通知后台");
    }

    public ResultMessage getResult() {
        return result;
    }

    public void setResult(ResultMessage result) {
        this.result = result;
    }

    public static APPResultMessage NO_LOGIN()
    {
        return new APPResultMessage(1001,"未登录");
    }

    public static APPResultMessage NO_ASPECT()
    {
        return new APPResultMessage(8999,"未授权");
    }

    public static APPResultMessage DATA_NOT_EXIST()
    {
        return new APPResultMessage(1010,"数据不存在");
    }

    public static APPResultMessage METHOD_WRONG()
    {
        return new APPResultMessage(1800,"请求方法不匹配，切换下get/post试下");
    }

    public static APPResultMessage NO_AUTH(){
        return new APPResultMessage(1999,"没有权限访问");
    }

    public static APPResultMessage Illegal_PARAMETER() {
        return new APPResultMessage(1012, "非法参数");
    }

    public static APPResultMessage NOT_ENOUGH() {
        return new APPResultMessage(1033, "账户余额不足");
    }

    public static APPResultMessage CODE_NOT_EXISTS() {
        return new APPResultMessage(1122, "验证码不存在");
    }

    public static APPResultMessage ALREADY_EXPIRE() {
        return new APPResultMessage(1011, "验证码已失效");
    }

    public static APPResultMessage REPEAT_OPERATE() {
        return new APPResultMessage(1044, "请勿重复操作");
    }

    public static APPResultMessage CHECK_PARAMS_ERROR(String msg) {
        return new APPResultMessage(1111, msg);
    }

    public static APPResultMessage getSuccessMessage(ResultMessage data)
    {
        APPResultMessage message = getSuccessMessage();
        message.setResult(data);
        return message;
    }

    public static APPResultMessage getSuccessMessage(String success)
    {
        return success == null ? new APPResultMessage(ResultEnum.SUCCESS.getCode(),"SUCCESS") : new APPResultMessage(ResultEnum.SUCCESS.getCode(),success);
    }

    public static APPResultMessage getErrorMessage(String error)
    {
        return error == null ? new APPResultMessage(666,"ERROR") : new APPResultMessage(666, error);
    }

    public static APPResultMessage getErrorMessage()
    {
        return getErrorMessage(null);
    }

    public int getKey() {
        return key;
    }

    public void setKey(int key) {
        this.key = key;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public static APPResultMessage ALREADY_DEL() {
        return new APPResultMessage(1006, "已经被删除");
    }

    public enum ResultEnum {

        SUCCESS(1000,"SUCCESS"),
        ERROR(2000,"ERROR");

        private int code;

        private String message;

        private ResultEnum(int code,String message)
        {
            this.code = code;
            this.message = message;
        }

        public int getCode()
        {
            return code;
        }
    }

}
